<?php
require_once 'PHPUnit/Framework.php';

require_once 'Cameo/SMS.php';
require_once 'Cameo/SMS/Message.php';

class Cameo_SMS_GatewayTests extends PHPUnit_Framework_TestCase {
	
	/**
	 * @var Cameo_SMS_Gateway_Test
	 */
	protected $_gateway;
	
	/**
	 * @var Cameo_SMS_Message
	 */
	protected $_message;
	
	protected $_sample_message_data = array(
		'to' => '12345',
		'text' => 'body text',
		'from' => 'Cameo SMS tester',
		'unicode' => true
	);
	
	public function setUp() {
		$this->_gateway = Cameo_SMS::factory('Test');
		$this->_message = $this->_gateway->createMessage();
	}
	
	public function testSendMessageWithNoBody() {
		$this->setExpectedException(
			'Cameo_SMS_Exception', 'Cannot send the message, there is no text in the message body'
		);
		
		$this->_message->setTo($this->_sample_message_data['to']);
		
		$this->_gateway->send($this->_message);
	}
	
	public function testSendMessageWithNoDestination() {
		$this->setExpectedException(
			'Cameo_SMS_Exception', 'Cannot send the message, there is no destination in the message'
		);
		
		$this->_message->setText($this->_sample_message_data['text']);
		
		$this->_message->send();
	}
	
	public function testBatchMessageWithNoDestination() {
		$this->setExpectedException(
			'Cameo_SMS_Exception', 'Cannot send the messages, there is no destination in for the batch'
		);
		
		$this->_message->setText($this->_sample_message_data['text']);
		
		$this->_message->batch(array());
	}
	
	public function testSendMessageWithNoFrom() {
		$this->setExpectedException(
			'Cameo_SMS_Exception', 'Cannot send the message, there is no source in the message'
		);
		
		$this->_message->setText($this->_sample_message_data['text']);
		$this->_message->setTo($this->_sample_message_data['to']);
		$this->_message->setFrom('');
		
		$this->_message->send();
	}
	
	public function testSendMessageWithInvalidTo() {
		$this->setExpectedException(
			'Cameo_SMS_Exception', 'Cannot send the message, the destination provided is not numeric'
		);
		
		$this->_message->setText($this->_sample_message_data['text']);
		$this->_message->setTo('invalid to');
		
		$this->_message->send();
	}
	
	public function testBatchDefaultMethod() {
		$this->_message->setText($this->_sample_message_data['text']);
		$this->_message->setFrom($this->_sample_message_data['to']);
		$resp = $this->_message->batch(array(
			'123',
			'234'
		));
		
		$this->assertEquals(2, count($resp));
		
		$k = 0;
		$this->assertArrayHasKey('to', $resp[$k]);
		$this->assertEquals('123', $resp[$k]['to']);
		$this->assertArrayHasKey('apimsgid', $resp[$k]);
		
		$k = 1;
		$this->assertArrayHasKey('to', $resp[$k]);
		$this->assertEquals('234', $resp[$k]['to']);
		$this->assertArrayHasKey('apimsgid', $resp[$k]);
	}
	
	public function testSetRequiredFeatures() {
		$this->_gateway->setRequiredFeatures(32);
		
		$req = $this->_gateway->getRequiredFeatures();
		
		$this->assertType('array', $req);
		$this->assertEquals(1, count($req));
		$this->assertEquals(32, $req[0]);
	}
	
	public function testAddRequiredFeatures() {
		$this->_gateway->addRequiredFeature(32);
		$this->_gateway->addRequiredFeature(64);
		
		$req = $this->_gateway->getRequiredFeatures();
		
		$this->assertType('array', $req);
		$this->assertEquals(2, count($req));
		$this->assertEquals(32, $req[0]);
		$this->assertEquals(64, $req[1]);
	}
	
	public function testSetOptionsAtConstruct() {
		$gateway = Cameo_SMS::factory('Test', array(
			Cameo_SMS_Gateway_Abstract::OPT_DELIVERY_ACK => false,
			Cameo_SMS_Gateway_Abstract::OPT_FEATURES_REQUIRED => 32,
			Cameo_SMS_Gateway_Abstract::OPT_USERNAME  => 'gateway_username',
			Cameo_SMS_Gateway_Abstract::OPT_PASSWORD  => 'gateway_password'
		));
		
		$this->assertEquals(array(32), $gateway->getRequiredFeatures());
		$this->assertFalse($gateway->deliveryAcknowledgement());
		$this->assertEquals('gateway_username', $gateway->getUsername());
		$this->assertEquals('gateway_password', $gateway->getPassword());
	}
	
	public function testOtherAccessors() {
		$this->_gateway->setUsername('gateway_username');
		$this->_gateway->setPassword('gateway_password');
		$this->_gateway->setDeliveryAcknowledgement(true);
		
		$this->assertEquals('gateway_username', $this->_gateway->getUsername());
		$this->assertEquals('gateway_password', $this->_gateway->getPassword());
		$this->assertEquals(true, $this->_gateway->deliveryAcknowledgement());
	}
	
	public function testSendMessageWithOptions() {
		$this->_gateway->setDeliveryAcknowledgement(true);
		$this->_gateway->addRequiredFeature(32);
		$this->_gateway->addRequiredFeature(64);
		
		$message = $this->_gateway->createMessage();
		$message->setText($this->_sample_message_data['text']);
		$message->setTo($this->_sample_message_data['to']);
		
		$message->send();
		
		$this->assertEquals($this->_sample_message_data['text'], $this->_gateway->sent_messages[0]['text']);
		$this->assertEquals($this->_sample_message_data['to'], $this->_gateway->sent_messages[0]['to']);
		$this->assertEquals(true, $this->_gateway->sent_messages[0][Cameo_SMS_Gateway_Abstract::OPT_DELIVERY_ACK]);
		$this->assertEquals(array(32, 64), $this->_gateway->sent_messages[0][Cameo_SMS_Gateway_Abstract::OPT_FEATURES_REQUIRED]);
	}
	
}